const { recAddress } = require('../model')

// 查询某一用户的收货地址
exports.findAll = async (req, res) => {
	const { userId } = req.session
	const { rows, count } = await recAddress.findAndCountAll({
		where: { userId },
		raw: true,
	})
	return res.send({
		code: 1,
		data: rows,
		count,
	})
}

// 更新收货地址
exports.update = async (req, res) => {
	const { id } = req.body
	const params = req.body
	await recAddress.update(params, {
		where: { id },
	})
	return res.send({
		code: 1,
	})
}

// 新增收货地址
exports.create = async (req, res) => {
	const params = req.body
	params.userId = req.session.userId
	await recAddress.create(params)
	return res.send({
		code: 1,
	})
}

// 删除收货地址
exports.delete = async (req, res) => {
	const { id } = req.body
	await recAddress.destroy({
		where: { id },
	})
	return res.send({
		code: 1,
	})
}
